﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKHACHHANG.DanhMucForm;
using QLKHACHHANG.DataAccess;

namespace QLKHACHHANG
{
    public partial class frmCustomerConfig : Form
    {
        frmCustomer rootform;
        bool isupdate = false;
        public frmCustomerConfig(frmCustomer root, bool isup)
        {
            InitializeComponent();
            rootform = root;
            isupdate = isup;            
            if (isupdate)
            {
                CustomerInfo();
            }
        }
        public void SetTitle(string name)
        {
            this.Text = name;
        }      

        private void frmCustomerConfig_Load(object sender, EventArgs e)
        {
               
            // load danh sách loại khách hàng
           // CboUtil.SetDataSource(cbLoaikh, ConnectDB.GetDataTable("select * from tbl_LoaiKH"), "Maloaikh", "Tenloaikh", 0);
           // CboUtil.SetDataSource(cbDanhxung, ConnectDB.GetDataTable("select * from tbl_DanhXung"), "Madanhxung", "Tendanhxung", 0);
            try
            {
                // TODO: This line of code loads data into the 'ds_DanhXung.tbl_DanhXung' table. You can move, or remove it, as needed.
                this.tbl_DanhXungTableAdapter.Connection.ConnectionString = SetConfig.getAppSetting("ConnectionString");
                this.tbl_DanhXungTableAdapter.Fill(this.ds_DanhXung.tbl_DanhXung);
                // TODO: This line of code loads data into the 'ds_LoaiKH.tbl_LoaiKH' table. You can move, or remove it, as needed.
                this.tbl_LoaiKHTableAdapter.Connection.ConnectionString = SetConfig.getAppSetting("ConnectionString");
                this.tbl_LoaiKHTableAdapter.Fill(this.ds_LoaiKH.tbl_LoaiKH);    
            }
            catch (Exception ex)
            {
                MessageBox.Show("Vui lòng kiểm tra lại ! \n\n (" + ex.Message + ")", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        // show thong tin
        private void CustomerInfo()
        {
            txtMakh.Enabled = false;
            txtMakh.Text = rootform.gvKhachhang.GetFocusedDataRow()["Makh"].ToString();            
            txtTenkh.Text = rootform.gvKhachhang.GetFocusedDataRow()["Tenkh"].ToString();
            txtTenkh1.Text = rootform.gvKhachhang.GetFocusedDataRow()["Tenkhac"].ToString();
            lkLoaikh.EditValue = rootform.gvKhachhang.GetFocusedDataRow()["Maloaikh"].ToString();
            txtMST.Text = rootform.gvKhachhang.GetFocusedDataRow()["Masothue"].ToString();
            txtDiachi.Text = rootform.gvKhachhang.GetFocusedDataRow()["Diachigiaodich"].ToString();
            txtNguoilh.Text = rootform.gvKhachhang.GetFocusedDataRow()["Nguoilienhe"].ToString();
            lkDanhxung.EditValue = rootform.gvKhachhang.GetFocusedDataRow()["Madanhxung"].ToString();
            txtVitri.Text = rootform.gvKhachhang.GetFocusedDataRow()["Tenvitri"].ToString();            
            dtNgaysinh.DateTime = (DateTime)rootform.gvKhachhang.GetFocusedDataRow()["Ngaysinh"];
            txtCMT.Text = rootform.gvKhachhang.GetFocusedDataRow()["Socmnd"].ToString();
            txtSoDT.Text = rootform.gvKhachhang.GetFocusedDataRow()["Sodienthoai"].ToString();
            txtEmail.Text = rootform.gvKhachhang.GetFocusedDataRow()["Email"].ToString();
            txtSofax.Text = rootform.gvKhachhang.GetFocusedDataRow()["Fax"].ToString();
            txtGhichu.Text = rootform.gvKhachhang.GetFocusedDataRow()["Ghichu"].ToString();            
        }
        // update ds loai khach hang
        public void UpdateLoaiKH()
        {
                            
           // CboUtil.SetDataSource(cbLoaikh, ConnectDB.GetDataTable("select * from tbl_LoaiKH"), "Maloaikh", "Tenloaikh", 0);
             try
             {
                 this.tbl_LoaiKHTableAdapter.Fill(this.ds_LoaiKH.tbl_LoaiKH);  
             }
             catch (Exception ex)
             {
                 MessageBox.Show("Cập nhật danh sách thất bại ! \n\n (" + ex.Message + ")", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
            
        }
        // update ds danh xung
        public void UpdateDanhxung()
        {            
            //CboUtil.SetDataSource(cbDanhxung, ConnectDB.GetDataTable("select * from tbl_DanhXung"), "Madanhxung", "Tendanhxung", 0);        
            try
            {
                this.tbl_DanhXungTableAdapter.Fill(this.ds_DanhXung.tbl_DanhXung);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cập nhật danh sách thất bại ! \n\n (" + ex.Message + ")", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        
        private void btThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lkLoaikh_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                frmLoaiKH frm = new frmLoaiKH(this);
                frm.ShowDialog();
            }
        }

        private void lkDanhxung_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                frmDanhXung frm = new frmDanhXung(this);               
                frm.ShowDialog();
            }
        }

        private void frmCustomerConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát khỏi màn hình " + this.Text + " ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
            {
                e.Cancel = true;
            }           
        }
        private void ResetControl()
        {
            txtMakh.Focus();
            txtMakh.Text = "";
            txtTenkh.Text = "";
            txtTenkh1.Text = "";          
            lkLoaikh.EditValue = "-1";
            txtMST.Text = "";
            txtDiachi.Text = "";
            txtNguoilh.Text = "";
            lkDanhxung.EditValue = "-1";
            txtVitri.Text = "";
            dtNgaysinh.Text = "";
            txtCMT.Text = "";
            txtSoDT.Text = "";
            txtEmail.Text = "";
            txtSofax.Text = "";
            txtGhichu.Text = "";
            
        }       
        private void btLuu_Click(object sender, EventArgs e)
        {
            //lkLoaikh.EditValue.ToString();   
            if (isupdate)
            {
                // update khach hang
                if (DBCustomer.UpdateCustomer(txtMakh.Text, txtTenkh.Text, txtTenkh1.Text, lkLoaikh.EditValue.ToString(),
                    txtMST.Text, txtDiachi.Text, txtNguoilh.Text, lkDanhxung.EditValue.ToString(), txtVitri.Text,
                    dtNgaysinh.DateTime, txtCMT.Text, txtSoDT.Text, txtEmail.Text, txtSofax.Text, txtGhichu.Text))
                {
                    MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    rootform.UpdateView();

                }
            }
            else
            {
                if (txtMakh.Text.Equals("") || txtTenkh.Text.Equals("") || lkLoaikh.Text.Equals("") || txtNguoilh.Text.Equals("") || lkDanhxung.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng điền thông tin hàng đầy đủ. Dấu * là phần bắt buộc!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    
                }
                else
                {

                    // insert 1 khach hang moi
                    if (DBCustomer.InsertCustomer(txtMakh.Text, txtTenkh.Text, txtTenkh1.Text, lkLoaikh.EditValue.ToString(),
                        txtMST.Text, txtDiachi.Text, txtNguoilh.Text, txtNguoilh.Text != "" ? lkDanhxung.EditValue.ToString() : "", txtVitri.Text,
                    dtNgaysinh.DateTime, txtCMT.Text, txtSoDT.Text, txtEmail.Text, txtSofax.Text, txtGhichu.Text))
                    {
                        MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        rootform.UpdateView();
                        ResetControl();

                    }

                }
            }
        }
    }
}
